package com.gitee.wsl.collections.iterator


class LimitIterator<E>(
    private val it: Iterator<E>,
    private val limit: Long
) : Iterator<E> {
    private var count = 0

    override fun hasNext(): Boolean {
        return count <= limit && it.hasNext()
    }

    override fun next(): E {
        if (count < limit) {
            val o = it.next()
            count++
            return o
        }
        throw NoSuchElementException("iterator count exceed limit: count=$count,limit=$limit")
    }
}